/**
 * Copyright (C), 2015-2020, XXX有限公司 FileName: BookController Author: xiao Date: 2020/4/1 9:42 下午
 * History: <author> <time> <version> <desc> 作者姓名 修改时间 版本号 描述
 */
package com.hsiao.springboot.template.thymeleaf.controller;

import com.hsiao.springboot.template.thymeleaf.entity.Book;
import com.hsiao.springboot.template.thymeleaf.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Book 控制层
 *
 * @projectName springboot-parent
 * @title: BookController
 * @description: TODO
 * @author xiao
 * @create 2020/4/1
 * @sinc0.0
 */
@RestController
@RequestMapping(value = "/book")
public class BookController {

  private static final String BOOK_FORM_PATH_NAME = "book";
  private static final String BOOK_LIST_PATH_NAME = "books";
  private static final String REDIRECT_TO_BOOK_URL = "redirect:/book";

  @Autowired BookService bookService;

  /** 获取 Book 列表 处理 "/book" 的 GET 请求，用来获取 Book 列表 */
  @RequestMapping(method = RequestMethod.GET)
  public String getBookList(ModelMap map) {
    map.addAttribute("bookList", bookService.findAll());
    return BOOK_LIST_PATH_NAME;
  }

  /** 获取创建 Book 表单 */
  @RequestMapping(value = "/create", method = RequestMethod.GET)
  public String createBookForm(ModelMap map) {
    map.addAttribute("book", new Book());
    map.addAttribute("action", "create");
    return BOOK_FORM_PATH_NAME;
  }

  /**
   * 创建 Book 处理 "/book/create" 的 POST 请求，用来新建 Book 信息 通过 @ModelAttribute 绑定表单实体参数，也通过 @RequestParam
   * 传递参数
   */
  @RequestMapping(value = "/create", method = RequestMethod.POST)
  public String postBook(@ModelAttribute Book book) {
    bookService.insertByBook(book);
    return REDIRECT_TO_BOOK_URL;
  }

  /**
   * 获取更新 Book 表单 处理 "/book/update/{id}" 的 GET 请求，通过 URL 中的 id 值获取 Book 信息 URL 中的 id
   * ，通过 @PathVariable 绑定参数
   */
  @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
  public String getUser(@PathVariable Long id, ModelMap map) {
    map.addAttribute("book", bookService.findById(id));
    map.addAttribute("action", "update");
    return BOOK_FORM_PATH_NAME;
  }

  /** 更新 Book 处理 "/update" 的 PUT 请求，用来更新 Book 信息 */
  @RequestMapping(value = "/update", method = RequestMethod.POST)
  public String putBook(@ModelAttribute Book book) {
    bookService.update(book);
    return REDIRECT_TO_BOOK_URL;
  }

  /** 删除 Book 处理 "/book/{id}" 的 GET 请求，用来删除 Book 信息 */
  @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
  public String deleteBook(@PathVariable Long id) {
    bookService.delete(id);
    return REDIRECT_TO_BOOK_URL;
  }
}
